package com.example.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class BeanCopyUtils {

    private BeanCopyUtils() {}

    /**
     * 属性复制
     * @param source
     * @param cls
     * @return
     * @param <T>
     */
    public static <T>  T copyBean(Object source,Class<T> cls) {
        T result = null;
        try {
            result = cls.newInstance();
            BeanUtils.copyProperties(source, result);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * hi
     * @param source
     * @param cls
     * @return
     * @param <T>
     * @param <V>
     */
    public static <T,V> List<T> copyBeanList(List<V> source,Class<T> cls) {
        List<T> lists = new ArrayList<>();
        for (V v : source) {
            lists.add(copyBean(v,cls));
        }
        return lists;
    }

}
